import { WhiteList } from '@/config';
import { getTitle } from '@/utils';
import { Route, RouteLocationNormalized, RouteLocationNormalizedLoaded } from 'vue-router';

/**
 * @description: 路由监听
 * @return {*}
 */
export const watchRoute = () => {
    const TagsViewStore = tagsViewStore();
    const router = useRouter();
    /**
     * 判断标签列表中是否存在该标签
     */
    const isTags = (path: string) =>
        TagsViewStore.tagsViewList.some((tag: Route) => tag.fullPath === path);

    /**
     * 当前路由是否存在放行白名单中
     */
    const isPass = (path: string) => WhiteList.includes(path);

    router.afterEach(to => {
        if (!isPass(to.path)) TagsViewStore.setActiveRoute(to as any); // 避免 login, 404页面执行添加激活路由
        if (isPass(to.path) || isTags(to.fullPath)) return;
        const { fullPath, meta, name, params, path, query } = to;
        TagsViewStore.addTagsViewList({
            fullPath,
            meta,
            name,
            params,
            path,
            query,
            title: getTitle(to)
        } as any);
    });
};

/**
 * @description: 设置路由动画
 */
export const setTransitionName = (
    to: RouteLocationNormalized,
    from: RouteLocationNormalizedLoaded
) => {
    const toDepth = to.path.split('/').length;
    const fromDepth = from.path.split('/').length;
    to.meta.transition = toDepth <= fromDepth ? 'slide-right' : 'slide-left';
};
